<template>
  <upload-excel :on-success="handleSuccess" />
</template>

<script>
import { importEmployee } from '@/api/employee'
import { formatExcelDate } from '@/utils/index'
export default {
  name: 'Import',
  methods: {
    transExcel(results, header) {
      const mapInfo = {
        '入职日期': 'timeOfEntry',
        '手机号': 'mobile',
        '姓名': 'username',
        '转正日期': 'correctionTime',
        '工号': 'workNumber',
        '部门': 'departmentName',
        '聘用形式': 'formOfEmployment'
      }
      const res = results.map(item => {
        const zhkeys = Object.keys(item)
        const obj = {}
        zhkeys.forEach(zhkey => {
          const enkey = mapInfo[zhkey]
          obj[enkey] = item[zhkey]
          if (enkey === 'timeOfEntry' || enkey === 'correctionTime') {
            obj[enkey] = new Date(formatExcelDate(item[zhkey]))
          } else {
            obj[enkey] = item[[zhkey]]
          }
        })
        return obj
      })
      return res
    },
    async handleSuccess({ header, results }) {
      console.log(header, results)
      // 1. 按照接口文档的要求，组装数据
      const data = this.transExcel(results, header)
      console.log(data)
      // 2. 调用接口，传递数据
      const res = await importEmployee(data)
      console.log('exios表格', res)
      this.$message.success(res.data.message)
      // 路由跳转回之前的页面
      this.$router.back()
    }
  }
}
</script>
